﻿using System.Security.Principal;
using System.Threading.Tasks;
using Volo.Abp.Security.Claims;

namespace Xms.Abp.Features;

public class EditionFeaturesValueProvider : FeaturesValueProvider
{
    public const string ProviderName = "E";

    public override string Name => ProviderName;

    protected ICurrentPrincipalAccessor PrincipalAccessor;

    public EditionFeaturesValueProvider(IFeaturesStore featureStore, ICurrentPrincipalAccessor principalAccessor)
        : base(featureStore)
    {
        PrincipalAccessor = principalAccessor;
    }

    public override async Task<string> GetOrNullAsync(FeatureValueDefinition feature)
    {
        var editionId = PrincipalAccessor.Principal?.FindEditionId();
        if (editionId == null)
        {
            return null;
        }

        return await FeaturesStore.GetOrNullAsync(feature.Name, Name, editionId.Value.ToString());
    }
}
